package com.example.smsquiz;

import android.content.res.Resources;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Dmitry Kuyanov
 * Date: 15.03.13
 * Time: 10:13
 */
public class WebClient {

    Resources resources = Resources.getSystem();

    public ArrayList<String> getLastQuestions(int siteID, int questionsCount){
        ArrayList<String> questions = new ArrayList<String>();

        HttpClient httpclient = new DefaultHttpClient();
        String requestString = createRequestString(siteID,  questionsCount);
        HttpGet httpget = new HttpGet(requestString);

        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream content = entity.getContent();
                String result = convertStreamToString(content);
                JSONObject json = new JSONObject(result);

                JSONArray nameArray = json.names();
                JSONArray valArray = json.toJSONArray(nameArray);

                String infoLog = valArray.getString(0);
                AppLogger.Write(infoLog);

                JSONArray jsonQuestions = valArray.getJSONArray(1);
                for (int i = 0; i < jsonQuestions.length(); i++){
                    questions.add(jsonQuestions.getString(i));
                }
                content.close();
            }

        } catch (Exception e) {
            AppLogger.WriteError(e);
        }

        return questions;
    }

    private String createRequestString(int siteID, int questionsCount){
        StringBuilder builder = new StringBuilder();
        builder.append(resources.getString(R.string.service_address));
        builder.append("/");
        builder.append(resources.getString(R.string.service_method));
        builder.append("/?");
        builder.append(resources.getString(R.string.service_param_site));
        builder.append("=");
        builder.append(siteID);
        builder.append("&?");
        builder.append(resources.getString(R.string.service_param_count));
        builder.append("=");
        builder.append(questionsCount);
        return builder.toString();
    }

    private String convertStreamToString(InputStream is)
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}
